package com.waypolice.manager.service.impl;

import com.waypolice.base.BaseServiceImpl;
import com.waypolice.common.annotation.BaseService;
import com.waypolice.common.pojo.TaotaoResult;
import com.waypolice.manager.service.UpmsUserRoleService;
import com.waypolice.mapper.UpmsRoleMapper;
import com.waypolice.mapper.UpmsUserMapper;
import com.waypolice.mapper.UpmsUserOrganizationMapper;
import com.waypolice.mapper.UpmsUserRoleMapper;
import com.waypolice.pojo.*;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* UpmsUserRoleService实现
* Created by shuzheng on 2017/3/20.
*/
@Service
@Transactional
@BaseService
public class UpmsUserRoleServiceImpl extends BaseServiceImpl<UpmsUserRoleMapper, UpmsUserRole, UpmsUserRoleExample> implements UpmsUserRoleService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UpmsUserRoleServiceImpl.class);

    @Autowired
    UpmsUserRoleMapper upmsUserRoleMapper;
    @Autowired
    UpmsRoleMapper upmsRoleMapper;
    @Autowired
    UpmsUserMapper upmsUserMapper;
    @Autowired
    UpmsUserOrganizationMapper upmsUserOrganizationMapper;

    @Override
    public int role(String[] roleIds, int id) {
        int result = 0;
        // 删除旧记录
        UpmsUserRoleExample upmsUserRoleExample = new UpmsUserRoleExample();
        upmsUserRoleExample.createCriteria()
                .andUserIdEqualTo(id);
        upmsUserRoleMapper.deleteByExample(upmsUserRoleExample);
        // 增加新记录
        if (null != roleIds) {
            for (String roleId : roleIds) {
                if (StringUtils.isBlank(roleId)) {
                    continue;
                }
                UpmsUserRole upmsUserRole = new UpmsUserRole();
                upmsUserRole.setUserId(id);
                upmsUserRole.setRoleId(NumberUtils.toInt(roleId));
                result = upmsUserRoleMapper.insertSelective(upmsUserRole);
            }
        }
        return result;
    }

    @Override
    public TaotaoResult findUserRole(Integer userId) {
        TaotaoResult result = new TaotaoResult();
        UpmsUserRoleExample upmsUserRoleExample = new UpmsUserRoleExample();
        UpmsUserRoleExample.Criteria criteria = upmsUserRoleExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<UpmsUserRole> upmsUserRoles = upmsUserRoleMapper.selectByExample(upmsUserRoleExample);
        for (UpmsUserRole upmsUserRole:upmsUserRoles) {
            Integer roleId = upmsUserRole.getRoleId();
            UpmsRole upmsRole = upmsRoleMapper.selectByPrimaryKey(roleId);
            if(upmsRole != null){
                if("arcManager".equals(upmsRole.getStr1())){
                    result.setStatus(200);
                    return result;
                }
            }
        }
        result.setStatus(101);
        return result;
    }

    /**
     * @author:zyp
     * @description 根据用户ID，获取所有角色
     * @date: 2019/7/31 15:26
     * @return
     */
    @Override
    public List<UpmsRole> selectRolesByUserId(Integer userId) {
        UpmsUserRoleExample userRoleExample = new UpmsUserRoleExample();
        UpmsUserRoleExample.Criteria criteria = userRoleExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<UpmsUserRole> upmsUserRoles = upmsUserRoleMapper.selectByExample(userRoleExample);
        List<Integer> rolesId = new ArrayList<>();
        for(UpmsUserRole upmsUserRole : upmsUserRoles){
            rolesId.add(upmsUserRole.getRoleId());
        }
        UpmsRoleExample upmsRoleExample = new UpmsRoleExample();
        UpmsRoleExample.Criteria criteria1 = upmsRoleExample.createCriteria();
        criteria1.andRoleIdIn(rolesId);
        List<UpmsRole> upmsRoles = upmsRoleMapper.selectByExample(upmsRoleExample);
        return upmsRoles;
    }

    /**
     * @author:zyp
     * @description 根据角色id 获取所有用户
     * @date: 2019/7/31 15:54
     * @return
     */
    @Override
    public List<Integer> selectRolesByRoleId(Integer roleId) {
        UpmsUserRoleExample userRoleExample = new UpmsUserRoleExample();
        UpmsUserRoleExample.Criteria criteria = userRoleExample.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        List<UpmsUserRole> upmsUserRoles = upmsUserRoleMapper.selectByExample(userRoleExample);
        List<Integer> userIds = new ArrayList<>();
        for(UpmsUserRole upmsUserRole : upmsUserRoles){
            userIds.add(upmsUserRole.getUserId());
        }
        return userIds;
    }

    
    /**
     * @author:zyp
     * @description 获取厅领导的所有用户
     * @date: 2019/7/31 16:28
     * @return 
     */
    @Override
    public List<Integer> selectRolesByTingLeader() {
        UpmsRoleExample upmsRoleExample = new UpmsRoleExample();
        UpmsRoleExample.Criteria upmsRoleExampleCriteria = upmsRoleExample.createCriteria();
        upmsRoleExampleCriteria.andNameEqualTo("厅领导");
        List<UpmsRole> upmsRoles = upmsRoleMapper.selectByExample(upmsRoleExample);
        UpmsUserRoleExample userRoleExample = new UpmsUserRoleExample();
        UpmsUserRoleExample.Criteria criteria = userRoleExample.createCriteria();
        criteria.andRoleIdEqualTo(upmsRoles.get(0).getRoleId());
        List<UpmsUserRole> upmsUserRoles = upmsUserRoleMapper.selectByExample(userRoleExample);
        List<Integer> userIds = new ArrayList<>();
        for(UpmsUserRole upmsUserRole : upmsUserRoles){
            userIds.add(upmsUserRole.getUserId());
        }
        return userIds;
    }

    /**
     * @author:zyp
     * @description 获取厅领导的所有用户
     * @date: 2019/7/31 16:28
     * @return
     */
    @Override
    public List<Integer> selectRolesByBuMenLeader(Integer userId) {
        UpmsUserOrganizationExample upmsUserOrganizationExample = new UpmsUserOrganizationExample();
        UpmsUserOrganizationExample.Criteria upmsUserOrganizationExampleCriteria = upmsUserOrganizationExample.createCriteria();
        upmsUserOrganizationExampleCriteria.andUserIdEqualTo(userId);
        //当前用户的管理部门id
        List<UpmsUserOrganization> upmsUserOrganizations = upmsUserOrganizationMapper.selectByExample(upmsUserOrganizationExample);
        List<Integer> finalUserIds = new ArrayList<>();
        if(upmsUserOrganizations==null||upmsUserOrganizations.size()==0){
            UpmsRoleExample upmsRoleExample = new UpmsRoleExample();
            UpmsRoleExample.Criteria upmsRoleExampleCriteria = upmsRoleExample.createCriteria();
            upmsRoleExampleCriteria.andNameEqualTo("部门领导");
            List<UpmsRole> upmsRoles = upmsRoleMapper.selectByExample(upmsRoleExample);
            UpmsUserRoleExample userRoleExample = new UpmsUserRoleExample();
            UpmsUserRoleExample.Criteria criteria = userRoleExample.createCriteria();
            criteria.andRoleIdEqualTo(upmsRoles.get(0).getRoleId());
            List<UpmsUserRole> upmsUserRoles = upmsUserRoleMapper.selectByExample(userRoleExample);
            finalUserIds = new ArrayList<>();
            for(UpmsUserRole upmsUserRole : upmsUserRoles){
                finalUserIds.add(upmsUserRole.getUserId());
            }
        }else{
            UpmsRoleExample upmsRoleExample = new UpmsRoleExample();
            UpmsRoleExample.Criteria upmsRoleExampleCriteria = upmsRoleExample.createCriteria();
            upmsRoleExampleCriteria.andNameEqualTo("部门领导");
            //当前部门领导角色的id
            List<UpmsRole> upmsRoles = upmsRoleMapper.selectByExample(upmsRoleExample);
            UpmsUserRoleExample userRoleExample = new UpmsUserRoleExample();
            UpmsUserRoleExample.Criteria criteria = userRoleExample.createCriteria();
            criteria.andRoleIdEqualTo(upmsRoles.get(0).getRoleId());
            List<UpmsUserRole> upmsUserRoles = upmsUserRoleMapper.selectByExample(userRoleExample);
            List<Integer> userIds = new ArrayList<>();
            //通过角色获取所有的用户id
            for(UpmsUserRole upmsUserRole : upmsUserRoles){
                userIds.add(upmsUserRole.getUserId());
            }
            //通过其中的用户id以及组织机构 精准的获取用户id
            UpmsUserOrganizationExample upmsUserOrganizationExample1 = new UpmsUserOrganizationExample();
            UpmsUserOrganizationExample.Criteria upmsUserOrganizationExampleCriteria1 = upmsUserOrganizationExample1.createCriteria();
            upmsUserOrganizationExampleCriteria1.andUserIdIn(userIds);
            upmsUserOrganizationExampleCriteria1.andOrganizationIdEqualTo(upmsUserOrganizations.get(0).getOrganizationId());
            //当前用户的管理部门id
            List<UpmsUserOrganization> upmsUserOrganizations1 = upmsUserOrganizationMapper.selectByExample(upmsUserOrganizationExample1);

            for(UpmsUserOrganization upmsUserOrganization : upmsUserOrganizations1){
                finalUserIds.add(upmsUserOrganization.getUserId());
            }
        }
        return finalUserIds;
    }

}